package com.etc.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.etc.entity.Orders;
import com.etc.entity.User;
import com.etc.service.OrdersService;
import com.etc.tools.Msg;
import com.etc.tools.PageData;

/**
 * 寄养订单控制器
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping(value = "orders")
public class OrdersController {
	@Resource(name = "ordersService")
	private OrdersService os;
	private Msg msg = new Msg();
	Logger logger=Logger.getRootLogger();

	/**
	 * 查询所有的订单
	 * 
	 * @return
	 */
	@RequestMapping(value = "list", method = RequestMethod.GET)
	@ResponseBody
	public PageData<Orders> getOrdersByPage(int page,int limit,String content) {
		if(content==null) {
			content="";
		}
		return os.getOrdersByPage(page, limit, content);
	}

	/**
	 * 根据用户编号查询订单（用户在个人中心查订单）
	 * 
	 * @return
	 */
	@RequestMapping(value = "queryOrder", method = RequestMethod.GET)
	public ModelAndView getOrdersByUid(int ouid) {
		List<Orders> orders= os.getOrdersByUid(ouid);
		ModelAndView mv=new ModelAndView("frontdevel/usercenter");
		mv.addObject("orders", orders);
		logger.info("************"+orders);
		return mv;
	}
	
	/**
	 * 根据用户编号查询订单（用户在个人中心查订单）
	 * 
	 * @return
	 */
	@RequestMapping(value = "queryOrderByname", method = RequestMethod.GET)
	public ModelAndView getOrdersByUname(String otuname) {
		List<Orders> orders= os.getOrdersByUname(otuname);
		ModelAndView mv=new ModelAndView("frontdevel/usercenter");
		mv.addObject("orders", orders);
		logger.info("************"+orders);
		return mv;
	}

	/**
	 * 修改订单信息
	 * 
	 * @param orders
	 * @return
	 */
	@RequestMapping(value = "updateOrder", method = RequestMethod.POST)
	@ResponseBody
	public Msg updateOrdersByOid(@RequestBody Orders orders) {
		boolean flag = os.updateOrderById(orders);
		msg.setMsg("修改失败");
		if (flag) {
			msg.setMsg("修改成功");
		}
		return msg;

	}

	/**
	 * 删除订单
	 * 
	 * @param oid
	 * @return
	 */
	@RequestMapping(value = "delOrder", method = RequestMethod.GET)
	@ResponseBody
	public Msg<String> delOrder(int oid) {
		msg.setMsg("删除失败");
		boolean flag = os.delOrderById(oid);
		if (flag) {
			msg.setMsg("删除成功");
		}
		return msg;
	}
	
	
	/**
	 * 后台增加订单
	 * @param orders
	 * @return
	 */
	@RequestMapping(value = "addOrder", method = RequestMethod.POST)
	@ResponseBody
	public Msg<String> AddOrder(@RequestBody Orders orders) {
		msg.setMsg("增加失败");
		boolean flag = os.addOrder(orders);
		if (flag) {
			msg.setMsg("增加成功");
		}
		return msg;
	}

	/**
	 * 前台首页预约寄养
	 * @param orders
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "frontAdd")
	@ResponseBody
	public boolean  FrontAdd(@RequestBody Orders order,HttpServletRequest request) {
		User user=(User) request.getSession().getAttribute("user");
		System.out.println(user);
		logger.info(order);
		boolean flag=os.addOrder(order);
		return flag;
	}
	
	
	/**
	 * 用户确认预约完成 修改订单状态
	 * @param orders
	 * @return
	 */
	@RequestMapping(value = "updateOstatus")
	@ResponseBody
	public Msg<String> updateOstatus(int oid) {
		boolean flag = os.updateStatus(oid);
		logger.info("flag******:"+flag+","+oid);
		if (flag) {
			msg.setMsg("订单完成");
		}
		return msg;
	}


}
